﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAOTicketReservationSystem;

namespace OnlineReservation
{
    public partial class WebFormPurchase : System.Web.UI.Page
    {
        private static int eventId = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            ValidatePage();
        }

        protected void ValidatePage()
        {
            if (Request.Params["EventID"] != null)
            {
                if (int.TryParse(Request.Params["EventID"], out eventId))
                {
                    DivPurchased.Visible = false;
                    Dao d = new Dao();
                    Event ev = d.GetEventById(eventId);
                    if (ev == null)
                    {
                        Response.Redirect("WebFormEvents.aspx");
                    }
                    else
                    {
                        LabelEventName.Text = ev.EventName;
                        LabelTicketPrice.Text = ev.Price.ToString();
                    }
                }
                else
                {
                    Response.Redirect("WebFormEvents.aspx");
                }
            }
            else
            {
                Response.Redirect("WebFormEvents.aspx");
            }
        }

        protected void ButtonBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("WebFormEvents.aspx");
        }

        protected void ButtonBuy_Click(object sender, EventArgs e)
        {
            Page.Validate("Buy");
            if (Page.IsValid)
            {
                string customerName = Server.HtmlEncode(TextBoxFullName.Text);
                int numberOfTickets = int.Parse(TextBoxTicketsNumber.Text);
                string address = Server.HtmlEncode(TextBoxShippingAddress.Text);
                int purchaseId = Dao.InsertPurchase(eventId, customerName, numberOfTickets, address);
                LabelPurchaseNumber.Text = purchaseId.ToString();
                DivPurchase.Visible = false;
                DivPurchased.Visible = true;
            }            
        }

        protected void OK_Click(object sender, EventArgs e)
        {
            Response.Redirect("WebFormEvents.aspx");
        }
    }
}